package org.huyue.huyuexia.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.huyue.huyuexia.mapper.ArticleMapper;
import org.huyue.huyuexia.pojo.Article;
import org.huyue.huyuexia.pojo.Query.ArticleQueryParam;
import org.huyue.huyuexia.service.ArticleService;
import org.huyue.huyuexia.util.CurrentHolder;
import org.huyue.huyuexia.util.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl  implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageResult findAll(ArticleQueryParam queryParam) {
        PageHelper.startPage(queryParam.getPageNum(), queryParam.getPageSize());
        List<Article> articleList = articleMapper.findAll();
        Page< Article> P = (Page< Article>) articleList;
        return new PageResult(P.getTotal(), P.getResult());
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.findById(id);
    }

    @Override
    public void update(Article article) {
        articleMapper.update( article);
    }

    @Override
    public void save(Article article) {
        article.setAuthorId(CurrentHolder.getCurrentId());
        articleMapper.add(article);
    }


}
